package com.yixin.ystartlibrary.helper;

import android.os.Handler;

/**
 * User: Luozi
 * Date: 2016-08-11
 * Content:
 */

public class SimpleTimer {

    private int mDelayMillis = DELAY_MILLIS;
    private static final int DELAY_MILLIS = 1000;

    private OnTimerBuzzListener mBuzzListener;

    public SimpleTimer() {
    }

    public SimpleTimer(int delayMillis) {
        mDelayMillis = delayMillis;
    }

    public void setOnTimerBuzzListener(OnTimerBuzzListener l) {
        mBuzzListener = l;
    }

    private Handler mHandler = new Handler();

    private Runnable mRunnable = new Runnable() {
        public void run() {
            if (mBuzzListener != null) {
                mBuzzListener.onBuzz();
            }
            mHandler.postDelayed(this, mDelayMillis);
        }
    };

    public void start() {
        mHandler.postDelayed(mRunnable, mDelayMillis); // 开始Timer
    }

    public void close() {
        mHandler.removeCallbacks(mRunnable); //停止Timer
    }

    public interface OnTimerBuzzListener {
        void onBuzz();
    }
}
